package Api;

import Util.HttpUtil;
import Util.MyKey;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static Util.Tools.MaptoStr;
import static Util.Tools.getMD5;

/**
 * 老收款查询Api 只能查三个月内的交易，用的md5加密，简单，但是建议用新的收款查询-------OrderQuery
 */

public class PayQueryApi {
    // 主函数，程序入口，执行支付查询
    public static void main(String[] args) {
        String url = "https://query.heepay.com/Payment/Query.aspx";
        String key = MyKey.MD5Key; // MD5密钥
        String agent_id = "1664502"; // 商户编号
        String agent_bill_id = "estart13006071111"; // 商户订单号
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); // 当前14位时间戳
        String remark = "1234"; // 商户自定义备注
        String return_mode = "1"; // 返回模式

        try {
            String sign = createSignature(agent_id, agent_bill_id, timestamp, return_mode, key);
            Map<String, String> parameters = createParameters(agent_id, agent_bill_id, timestamp, remark, return_mode, sign);
            String response = HttpUtil.sendPost1(url, parameters);
            processResponse(response, key);
        } catch (IOException ex) {
            System.err.println("请求发生异常: " + ex.getMessage());
        }
    }

    // 创建签名，返回MD5签名字符串
    private static String createSignature(String agent_id, String agent_bill_id, String agent_bill_time, String return_mode, String key) {
        String dataToSign = String.format("version=1&agent_id=%s&agent_bill_id=%s&agent_bill_time=%s&return_mode=%s&key=%s",
                agent_id, agent_bill_id, agent_bill_time, return_mode, key);
        System.out.println("待签名串：" + dataToSign);
        return getMD5(dataToSign);
    }

    // 创建请求参数的Map，准备发送请求
    private static Map<String, String> createParameters(String agent_id, String agent_bill_id, String agent_bill_time, String remark, String return_mode, String sign) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("version", "1");
        parameters.put("agent_id", agent_id);
        parameters.put("agent_bill_id", agent_bill_id);
        parameters.put("agent_bill_time", agent_bill_time);
        parameters.put("remark", remark);
        parameters.put("return_mode", return_mode);
        parameters.put("sign", sign);
        System.out.println("发送请求数据：" + MaptoStr(parameters));
        return parameters;
    }

    // 处理响应，进行验签
    private static void processResponse(String response, String key) {
        Map<String, String> responseParameters = new HashMap<>();
        for (String pair : response.split("\\|")) {
            String[] keyValue = pair.split("=");
            if (keyValue.length == 2) {
                responseParameters.put(keyValue[0], keyValue[1]);
            }
        }

        String dataToSign = createResponseDataToSign(responseParameters, key);
        System.out.println("待验签串：" + dataToSign);
        String sign2 = getMD5(dataToSign);

        if (!sign2.equals(responseParameters.get("sign"))) {
            System.out.println("同步验签结果：验签失败！");
        } else {
            System.out.println("同步验签结果：验签成功！");
        }
    }

    // 创建响应数据的待验签字符串
    private static String createResponseDataToSign(Map<String, String> responseParameters, String key) {
        return String.format("agent_id=%s|agent_bill_id=%s|jnet_bill_no=%s|pay_type=%s|result=%s|pay_amt=%s|pay_message=%s|remark=%s|key=%s",
                responseParameters.getOrDefault("agent_id", ""),
                responseParameters.getOrDefault("agent_bill_id", ""),
                responseParameters.getOrDefault("jnet_bill_no", ""),
                responseParameters.getOrDefault("pay_type", ""),
                responseParameters.getOrDefault("result", ""),
                responseParameters.getOrDefault("pay_amt", ""),
                responseParameters.getOrDefault("pay_message", ""),
                responseParameters.getOrDefault("remark", ""),
                key);
    }
}

